package com.croqodile{
    import flash.display.MovieClip;
    import flash.system.Security;
    import com.senocular.utils.Output;
    import flash.utils.Timer;
    import flash.utils.Dictionary;
    import com.croqodile.IslandReplica;
    import com.croqodile.IslandObject;
    import com.croqodile.Controller;
    import com.croqodile.FarRef;
    import flash.events.*;
    
    public class FarRefRegistry{
	
	private var _dict:Dictionary;
	private var _highestGuid:int = 0;
	private var _island:IslandReplica;
	
	public function FarRefRegistry(island:IslandReplica){
	    this._island = island;
	    this._dict = new Dictionary();
	}
	
	public function register(object:IslandObject):FarRef{
	    this._highestGuid += 1;
	    var ref:FarRef = new FarRef(this._highestGuid, 
					object, 
					this._island);
	    this._dict[this._highestGuid] = ref;
	    return ref;
	}
	
	public function lookupObjectByRef(ref:FarRef):IslandObject{
	    return this._dict[ref.guid()].target();
	}
	
	public function lookupObjectByGuid(guid:int):IslandObject{
	    try {
		return this._dict[guid].target();
	    }
	    catch(e:Error){
		throw new Error("Object for guid #" + guid + 
				" not found. Highest guid is " +
				this._highestGuid);
	    }
	    return null;
	}
	
    }
}


